#include "LocationPopMenu.h"




#include <QFont>
#include <QFontDatabase>
#include <QPainter>
#include <QSize>
#include <QPainterPath>

#include <QString>
#include <QTimer>

#include "../../uitools/IdRootMenu.h"
#include "../../uitools/vhc/constview4fun.h"
#include "../../uitools/vhc/constview4id.h"


#include <cstdio>

#include <QDebug>


#define Width_Dlg_thiz  124

namespace kepen {
namespace core {

int LocationConvertCode2Id(quint32 code)
{
    int ret ;
    switch(code)
    {
    case LocationByRoad_Code:
        ret = LocationByRoad;
        break;
    case Locaion_ByResident_Code:
        ret = Locaion_ByResident;
        break;
    case LocationByFootmark_Code:
        ret = LocationByFootmark;
        break;
    case Gps_by_input_Code:
        ret = Gps_by_input;
        break;
    default:
        ret = MenuId_None;
        break;
    }

    return ret;
}

LocationPopMenu::LocationPopMenu(QWidget *parent ,const QFont &font):
    QWidget{parent}
    ,btnMenu(NULL)
    ,isMouseLeftDown(false)
    ,m_nIdTouchedDown(MenuId_None)
    ,m_nIdMouseOver(MenuId_None)
{
    int height =Horizontal_Line_Seperate_Size + HEIGHT_DropCombox_CELL * 4, width = Width_Dlg_thiz;
    idMenuArray[0] = LocationByRoad;
    idMenuArray[1] = Locaion_ByResident;
    idMenuArray[2] = LocationByFootmark;
    idMenuArray[3] = Horizontal_Line_Seperate;
    idMenuArray[4] = Gps_by_input;

    // | Qt::FramelessWindowHint | Qt::NoDropShadowWindowHint
    setWindowFlags(Qt::Popup);

    // setGeometry(0,0,width,height);
    setFixedSize(width,height);


    fontIcon = font;




    setMouseTracking(true);
    // this->setFocus();
    // this->setAttribute(Qt::WA_ShowWithoutActivating);
    // resize(width,height);
    // QObject::installEventFilter(this);
}

LocationPopMenu::~LocationPopMenu()
{

}


void LocationPopMenu::timeoutClose()
{
    int idTouched = m_nIdTouchedDown;
    Q_EMIT onClickedMenuSignals(this,idTouched);



}



void LocationPopMenu::mouseMoveEvent(QMouseEvent *event)
{
    if(false == isMouseLeftDown){
        QPointF point = event->position();
        int nIdMouseOver = m_nIdMouseOver;
        m_nIdMouseOver = getIdOfTouched(point);
        if(nIdMouseOver != m_nIdMouseOver){
            repaint();
        }
    }
}


void  LocationPopMenu::mousePressEvent(QMouseEvent *event)
{
    Qt::MouseButton btn = event->button();
    if(Qt::MouseButton::LeftButton == btn)
    {
        QPointF point = event->position();
        ptDown = point;
        isMouseLeftDown = true;
        m_nIdTouchedDown = getIdOfTouched(point);
        m_nIdMouseOver = MenuId_None;
        repaint();
    }
}

void  LocationPopMenu::mouseReleaseEvent(QMouseEvent *)
{


    bool isMouseDown = isMouseLeftDown;
    // m_nIdTouchedDown = MenuId_None;
    m_nIdMouseOver = MenuId_None;
    isMouseLeftDown = false;
    if(isMouseDown)
    {
        // will be delete this
        int id = m_nIdTouchedDown;
        if(false == (Horizontal_Line_Seperate == id))
            QTimer::singleShot(TimeoutClickFree,this,SLOT(timeoutClose()));
    }

}

void LocationPopMenu::paintEvent(QPaintEvent *)
{

    int widthWindow ,heightWidow;
    QSize rectWin = this->size();
    QPainter painter(this);
    painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform | QPainter::TextAntialiasing |QPainter::VerticalSubpixelPositioning);


    widthWindow  = rectWin.width();
    heightWidow = rectWin.height();

    painter.fillRect(0,0,widthWindow ,heightWidow,QColor(QRgba64::fromArgb32(Background_PopWindow)));

    onPaintButtons(&painter,rectWin);

    QBrush brushBorder(Qt::darkGray,Qt::SolidPattern);
    QPen penBorder(brushBorder,1.0f, Qt::SolidLine);
    painter.setPen(penBorder);
    painter.setBrush(Qt::NoBrush);
    painter.drawRect(0,0,widthWindow ,heightWidow);
}


int  LocationPopMenu::getIdOfTouched(const QPointF &point)
{
    int nRet = MenuId_None;
    int x0,y0;
    x0 = qRound(point.x());
    y0 = qRound(point.y());

    if(x0 > 0 && x0 < Width_Dlg_thiz)
    {
        int nId,top, numBtns,  i = 0,bottom = 0;
        numBtns = sizeof(idMenuArray)/sizeof(idMenuArray[0]);

        for (; i < numBtns; ++i) {
            top = bottom;
            nId = idMenuArray[i];
            if(Horizontal_Line_Seperate == nId)
                bottom = top + Horizontal_Line_Seperate_Size;
            else
                bottom = top + HEIGHT_DropCombox_CELL;

            if (y0 >= top && y0 < bottom) {
                nRet = nId;
                break;
            }
        }
    }

    return nRet;
}


void  LocationPopMenu::onPaintButtons(QPainter *painter,const  QSize &rectWin)
{
    int _nID_touchedDown = m_nIdTouchedDown;
    int nIdMouseOver = m_nIdMouseOver;

    int i=0,count = sizeof(idMenuArray)/sizeof(idMenuArray[0]);
    int childHeight  ;

    int sizeTextIcon = 16;
    int id ,left,topOfLine = 0,childWidth = 40,dlgWidth =rectWin.width();
    bool isMouseOver,isTouchedDown;
    char szBuf[64];
    QFont fontLabel;
    fontLabel.setPointSize(TextSize_Tool);

    for(;i<count;++i)
    {
        id = idMenuArray[i];
        childHeight = Horizontal_Line_Seperate == id ? Horizontal_Line_Seperate_Size: HEIGHT_DropCombox_CELL;

        isMouseOver = (nIdMouseOver == id);
        isTouchedDown = (_nID_touchedDown == id);

        left = 0;




        if(Horizontal_Line_Seperate == id){

            int cy0 = topOfLine + ( childHeight>> 1) ;
            int dNleft = Horizontal_Line_Seperate_Margin,dNright = dlgWidth  - Horizontal_Line_Seperate_Margin;
            QBrush brushSeg(QColor(QRgba64::fromArgb32(Horizontal_Line_Seperate_Color)),Qt::SolidPattern);

            QPen pen(brushSeg,Horizontal_Line_Seperate_BorderWidth, Qt::SolidLine);

            painter->setBrush(Qt::BrushStyle::NoBrush);
            painter->setPen(pen);
            painter->drawLine(dNleft,cy0,dNright,cy0);

        }else {

            {
                if(isMouseOver)
                {
                    if(false == isTouchedDown){
                        //0x40f0fff0U Background_PopBtnOver
                        // QBrush brushBackOver(QColor(QRgba64::fromArgb32(Background_PopBtnOver)),Qt::SolidPattern);
                        // QBrush brBackOver(QColor(QRgba64::fromArgb32(Background_PopBtnOverR2)),Qt::SolidPattern);
                        QBrush brushBackOverR3(QColor(QRgba64::fromArgb32(Background_PopBtnOverR3)),Qt::SolidPattern);
                        QBrush brushBorder4Over(QColor(QRgba64::fromArgb32(BorderColor_PopBtnOver)),Qt::SolidPattern);
                        QPen penBorder4Over(brushBorder4Over,Horizontal_Line_Seperate_BorderWidth, Qt::SolidLine);

                        // painter->fillRect(left, topOfLine, dlgWidth,childHeight,brBackOver);

                        painter->setPen(penBorder4Over);
                        painter->setBrush(brushBackOverR3);
                        painter->drawRect(left, topOfLine, dlgWidth,childHeight);


/*
                        QPoint pos = QCursor::pos();
                        QScreen *screen = QGuiApplication::primaryScreen();
                        QPixmap pixmap = screen->grabWindow(0, pos.x(), pos.y(), 1, 1);
                        QImage image = pixmap.toImage();
                        QColor currentColor = image.pixelColor(0, 0);
                        QString sc= QString("background-color: %1").arg(currentColor.name());
                        qDebug().noquote() <<"-------File:" <<__FILE__ <<",Line:"<<__LINE__ << " "<<sc;//e5edee
*/
                    }
                }
                if(isTouchedDown){

                    QBrush brushBackDown(QColor(QRgba64::fromArgb32(Background_PopBtnDown)),Qt::SolidPattern);
                    painter->fillRect(left, topOfLine, dlgWidth,childHeight,brushBackDown);

                }
            }


            int widthLabel = dlgWidth - childWidth ;
            memset(szBuf,0,sizeof(szBuf));
            switch(id){
            case LocationByRoad:
                sprintf(szBuf,"%s","道路定位");
                break;
            case Locaion_ByResident:
                sprintf(szBuf,"%s","小区定位");
                break;
            case LocationByFootmark:
                sprintf(szBuf,"%s","我的足迹");
                break;
            case Gps_by_input:
            case Gps_by_phone:
                sprintf(szBuf,"%s","GPS坐标");
                break;
            }
            //= QStringLiteral

            {
                QString strLabelR2(szBuf);
                bool isChecked =false;



                  DRAW_ICON_FontR4(left,topOfLine,sizeTextIcon,id)




                    left = 28;
                cx0 = left + (widthLabel >> 1);
                cy0 = topOfLine + (childHeight >> 1);
                ntLeft = cx0 - 1000;
                ntTop = cy0 - 500;
                QRect rect4LabelR2(ntLeft, ntTop, 2000, 1000);
                QBrush brushLabelR2(QColor(QRgba64::fromArgb32(textColor)),Qt::SolidPattern);
                QPen penLabelText(brushLabelR2,1, Qt::SolidLine);
                painter->setFont(fontLabel);
                painter->setBrush(Qt::BrushStyle::NoBrush);
                painter->setPen(penLabelText);
                painter->drawText(rect4LabelR2,Qt::AlignCenter,strLabelR2);
            }



        }






        topOfLine += childHeight;
    }
}




}
}

